package reduce.fz.com.reduce.util;

import android.app.Activity;
import android.content.Intent;

import com.alibaba.sdk.android.callback.FailureCallback;
import com.duanqu.qupai.sdk.android.QupaiService;
import com.google.common.io.Files;

import java.io.File;
import java.io.IOException;
import java.util.Random;

import reduce.fz.com.reduce.bean.Video;
import reduce.fz.com.reduce.util.base.FileCache;
import reduce.fz.com.reduce.util.base.ReduceApplication;
import reduce.fz.com.reduce.util.oss.VideoOss;

/**
 * Created by qingshengzheng on 16/1/9.
 */
public class QuPaiOper {

    public Activity mActivity;
    public Boolean isGuideShow;
    public static final int RECORDE_SHOW = 1112;
    public QupaiService qupaiService;
    public VideoOss oss;


    public interface QuPaiListener {
        public void submitSuccess(Video video);

        public void submitFailed(Video video);
    }


    public static QuPaiOper instance(Activity activity, Boolean isGuideShow) {

        QuPaiOper oper = new QuPaiOper();
        oper.mActivity = activity;
        oper.isGuideShow = isGuideShow;
        oper.qupaiService = ReduceApplication.getInstance().getQupaiService();
        oper.oss = VideoOss.instance();
        return oper;
    }


    public void showRecordPage() {
        qupaiService.showRecordPage(mActivity, RECORDE_SHOW, isGuideShow,
                new FailureCallback() {
                    @Override
                    public void onFailure(int i, String s) {
                    }
                });
    }

    /**
     * 显示自定义请求code的录制页面
     *
     * @param requesCode
     */
    public void showRecordPage(int requesCode) {
        qupaiService.showRecordPage(mActivity, requesCode, isGuideShow,
                new FailureCallback() {
                    @Override
                    public void onFailure(int i, String s) {
                    }
                });
    }


    public Video dealIntent(Intent data) {
        if (data == null) return null;
        RecordResult result = new RecordResult(data);
        //得到视频地址，和缩略图地址的数组，返回十张缩略图
        String videoPath = result.getPath();
        String[] thum = result.getThumbnail();
        String newVideoPath = FileCache.getImageCacheDir(mActivity) + MD5.md5(videoPath) + "_" + new Random().nextInt(10000) + ".mp4";
        String newthumPath = FileCache.getVideoCacheDir(mActivity) + MD5.md5(videoPath) + "_" + new Random().nextInt(10000) + ".png";
        try {
            Files.move(new File(videoPath), new File(newVideoPath));
            Files.move(new File(thum[0]), new File(newthumPath));
            Video video = new Video();
            video.localPath = newVideoPath;
            video.localThumPath = newthumPath;
            qupaiService.deleteDraft(mActivity, data);
            return video;
        } catch (Exception e) {
        }
        return null;
    }


    public void submitVideo(Video video, final QuPaiListener quPaiListener) {
        oss.uploadVideo(video, new VideoOss.OssUploadResponse() {
            @Override
            public void onSuccess(Video video) {

                if (quPaiListener != null)
                    quPaiListener.submitSuccess(video);
            }

            @Override
            public void onFailed(Video video) {
                if (quPaiListener != null)
                    quPaiListener.submitFailed(video);

            }
        });
    }


}
